package top.codedance.iotp.tools.terminal.ui;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.tools.terminal.TerminalApplication;
import top.codedance.iotp.tools.terminal.bean.ComboBoxItem;
import com.fazecast.jSerialComm.SerialPort;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Enumeration;

/**
 *
 * @author xuedianfu
 */
public class AddConnection extends javax.swing.JDialog {
    
    private ConnectionManager cm;

    /**
     * Creates new form AddConnection
     */
    public AddConnection(ConnectionManager cm) {
        super(cm, true);
        this.cm = cm;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.cm.getShellFrame());
        setMinimumSize(new Dimension(421, 300));
        setTitle("代码飞舞采控系统-系统控制终端");
        initComUi();
        initData();
    }

    private void initComUi(){
        DefaultComboBoxModel<ComboBoxItem> cbm = new DefaultComboBoxModel<>();
        SerialPort[] ports = SerialPort.getCommPorts();
        for(SerialPort sp : ports) {
            cbm.addElement(new ComboBoxItem(sp.getSystemPortName() + "(" + sp.getDescriptivePortName() + " - " + sp.getPortDescription() + ")", sp.getSystemPortName()));
        }
        jcb_serial.setModel(cbm);
    }

    private void initData(){
        JSONObject selected = cm.getSelected();
        if(selected != null){
            Enumeration<AbstractButton> elements = this.type_bg.getElements();
            if(selected.containsKey("type")) {
                while (elements.hasMoreElements()) {
                    AbstractButton abstractButton = elements.nextElement();
                    if (abstractButton.getActionCommand().equals(selected.getString("type"))) {
                        abstractButton.setSelected(true);
                    } else {
                        abstractButton.setSelected(false);
                    }
                }
            }
            if(selected.containsKey("comProxy")){
                this.jcb_comProxy.setSelected(selected.getBoolean("comProxy"));
            }
            if(selected.containsKey("comInstanceNum")){
                this.jtf_comInstanceNum.setText(selected.getInteger("comInstanceNum").toString());
            }
            if(selected.containsKey("name")) {
                this.jtf_name.setText(selected.getString("name"));
            }
            if(selected.containsKey("url")) {
                this.jtf_url.setText(selected.getString("url"));
            }
            if(selected.containsKey("deviceId")) {
                this.jtf_device.setText(selected.getString("deviceId"));
            }
            if(selected.containsKey("username")) {
                this.jtf_username.setText(selected.getString("username"));
            }
            if(selected.containsKey("password")) {
                this.jtf_password.setText(selected.getString("password"));
            }
            if(selected.containsKey("com")) {
                JSONObject com = selected.getJSONObject("com");
                this.jcb_serial.getModel().setSelectedItem(new ComboBoxItem(com.getString("name"), com.getString("value")));
            }
            if(selected.containsKey("manage")) {
                this.jcb_isManage.setSelected(selected.getBoolean("manage"));
            }
            if(selected.containsKey("btl")) {
                this.jtf_btl.setText(selected.getString("btl"));
            }
            if(selected.containsKey("sjw")) {
                this.jtf_sjw.setText(selected.getString("sjw"));
            }
            if(selected.containsKey("tzw")) {
                this.jtf_tzw.setText(selected.getString("tzw"));
            }
            if(selected.containsKey("xyw")) {
                this.jtf_xyw.setText(selected.getString("xyw"));
            }
        }
    }

    public void cleanData(){
        this.type_bg.clearSelection();
        this.jtf_name.setText("");
        this.jtf_url.setText("");
        this.jtf_device.setText("");
        this.jtf_username.setText("");
        this.jtf_password.setText("");
        this.jcb_serial.setSelectedIndex(-1);
        this.jcb_isManage.setSelected(false);
        this.jtf_btl.setText("9600");
        this.jtf_sjw.setText("8");
        this.jtf_tzw.setText("1");
        this.jtf_xyw.setText("0");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        type_bg = new javax.swing.ButtonGroup();
        jl_name = new javax.swing.JLabel();
        jtf_name = new javax.swing.JTextField();
        jl_url = new javax.swing.JLabel();
        jtf_url = new javax.swing.JTextField();
        jl_device = new javax.swing.JLabel();
        jtf_device = new javax.swing.JTextField();
        jl_username = new javax.swing.JLabel();
        jtf_username = new javax.swing.JTextField();
        jl_password = new javax.swing.JLabel();
        jtf_password = new javax.swing.JTextField();
        jb_save = new javax.swing.JButton();
        jb_get = new javax.swing.JButton();
        type_http_jrb = new javax.swing.JRadioButton();
        type_com_jrb = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jcb_serial = new javax.swing.JComboBox<>();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jtf_btl = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtf_sjw = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtf_tzw = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtf_xyw = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jcb_comProxy = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        jtf_comInstanceNum = new javax.swing.JTextField();
        jcb_isManage = new javax.swing.JCheckBox();
        jrb_websocket = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);
        setMaximumSize(new java.awt.Dimension(421, 286));
        setMinimumSize(new java.awt.Dimension(421, 286));
        setResizable(false);
        setSize(new java.awt.Dimension(421, 286));

        jl_name.setText("客户端名称：");

        jl_url.setText("接口   地址：");

        jl_device.setText("设备   编号：");

        jl_username.setText("用户      名：");

        jl_password.setText("用户   密码：");

        jb_save.setText("保存");
        jb_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveActionPerformed(evt);
            }
        });

        jb_get.setText("获取当前配置");
        jb_get.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_getActionPerformed(evt);
            }
        });

        type_bg.add(type_http_jrb);
        type_http_jrb.setSelected(true);
        type_http_jrb.setText("HTTP连接");
        type_http_jrb.setToolTipText("");
        type_http_jrb.setActionCommand("http");

        type_bg.add(type_com_jrb);
        type_com_jrb.setText("COM连接");
        type_com_jrb.setActionCommand("com");

        jLabel1.setText("串    口：");
        jLabel1.setToolTipText("");

        jcb_serial.setToolTipText("");

        jLabel2.setText("波特率：");
        jLabel2.setToolTipText("");

        jtf_btl.setText("9600");
        jtf_btl.setToolTipText("");
        jtf_btl.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_btlKeyTyped(evt);
            }
        });

        jLabel3.setText("数据位：");
        jLabel3.setToolTipText("");

        jtf_sjw.setText("8");
        jtf_sjw.setToolTipText("");
        jtf_sjw.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_sjwKeyTyped(evt);
            }
        });

        jLabel4.setText("停止位：");
        jLabel4.setToolTipText("");

        jtf_tzw.setText("1");
        jtf_tzw.setToolTipText("");
        jtf_tzw.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_tzwKeyTyped(evt);
            }
        });

        jLabel5.setText("校验位：");
        jLabel5.setToolTipText("");

        jtf_xyw.setText("0");
        jtf_xyw.setToolTipText("");
        jtf_xyw.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_xywKeyTyped(evt);
            }
        });

        jcb_comProxy.setText("串口代理");
        jcb_comProxy.setToolTipText("");

        jLabel6.setText("代理实例编号：");

        jtf_comInstanceNum.setText("0");
        jtf_comInstanceNum.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtf_comInstanceNumKeyTyped(evt);
            }
        });

        jcb_isManage.setSelected(true);
        jcb_isManage.setText("设备管理");
        jcb_isManage.setToolTipText("选中使用设备管理协议，不选使用串口直连协议");

        type_bg.add(jrb_websocket);
        jrb_websocket.setText("WebSocket连接");
        jrb_websocket.setActionCommand("websocket");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(19, 19, 19)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(type_com_jrb)
                                    .addGap(18, 18, 18)
                                    .addComponent(jcb_isManage))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGap(29, 29, 29)
                                            .addComponent(jb_get)
                                            .addGap(18, 18, 18)
                                            .addComponent(jb_save)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jcb_serial, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(29, 29, 29))))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtf_xyw))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtf_tzw))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtf_sjw))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jtf_btl, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(18, 18, 18)
                                    .addComponent(jl_url))
                                .addComponent(jl_device, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jl_username, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jl_password, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jtf_username, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                                .addComponent(jtf_device, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jtf_url, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jtf_password)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(24, 24, 24)
                            .addComponent(jl_name)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jtf_name, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jrb_websocket)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jSeparator3))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(type_http_jrb)
                                    .addGap(18, 18, 18)
                                    .addComponent(jcb_comProxy)
                                    .addGap(18, 18, 18)
                                    .addComponent(jLabel6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jtf_comInstanceNum, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_name)
                    .addComponent(jtf_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jrb_websocket)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(type_http_jrb)
                    .addComponent(jcb_comProxy)
                    .addComponent(jLabel6)
                    .addComponent(jtf_comInstanceNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_url)
                    .addComponent(jtf_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_device)
                    .addComponent(jtf_device, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_username)
                    .addComponent(jtf_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_password)
                    .addComponent(jtf_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(type_com_jrb)
                    .addComponent(jcb_isManage))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jcb_serial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtf_btl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtf_sjw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtf_tzw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtf_xyw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_get)
                    .addComponent(jb_save))
                .addGap(12, 12, 12))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveActionPerformed
        String name = this.jtf_name.getText();
        ButtonModel selection = this.type_bg.getSelection();
        if(selection != null) {
            if (!name.isEmpty()) {
                JSONObject jo = new JSONObject();
                jo.put("type", selection.getActionCommand());
                jo.put("comProxy", jcb_comProxy.isSelected());
                jo.put("comInstanceNum", Integer.valueOf(this.jtf_comInstanceNum.getText()));
                jo.put("name", name);
                jo.put("url", this.jtf_url.getText());
                jo.put("deviceId", this.jtf_device.getText());
                jo.put("username", this.jtf_username.getText());
                jo.put("password", this.jtf_password.getText());
                ComboBoxItem selectedItem = (ComboBoxItem)this.jcb_serial.getSelectedItem();
                JSONObject com = new JSONObject();
                com.put("name", selectedItem != null ? selectedItem.getName() : "");
                com.put("value", selectedItem != null ? selectedItem.getValue() : "");
                jo.put("com", com);
                jo.put("manage", this.jcb_isManage.isSelected());
                jo.put("btl", this.jtf_btl.getText());
                jo.put("sjw", this.jtf_sjw.getText());
                jo.put("tzw", this.jtf_tzw.getText());
                jo.put("xyw", this.jtf_xyw.getText());
                jo.put("manage", this.jcb_isManage.isSelected());
                cm.addSave(jo);
            } else {
                JOptionPane.showMessageDialog(rootPane, "名称不能为空");
            }
        }else{
            JOptionPane.showMessageDialog(rootPane, "连接类型必须选一个");
        }
    }//GEN-LAST:event_jb_saveActionPerformed

    private void jb_getActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_getActionPerformed
        JSONObject currentConnection = this.cm.getCurrentConnection();
        String type = currentConnection.getString("type");
        Enumeration<AbstractButton> elements = this.type_bg.getElements();
        while (elements.hasMoreElements()) {
            AbstractButton abstractButton = elements.nextElement();
            if (abstractButton.getActionCommand().equals(type)) {
                abstractButton.setSelected(true);
            } else {
                abstractButton.setSelected(false);
            }
        }
        if(type.equals(this.type_http_jrb.getActionCommand())) {
            this.jtf_url.setText(currentConnection.getString("url"));
            this.jtf_device.setText(currentConnection.getString("deviceId"));
            this.jtf_username.setText(currentConnection.getString("username"));
            this.jtf_password.setText(currentConnection.getString("password"));
            this.jcb_comProxy.setSelected(currentConnection.getBoolean("comProxy"));
            if(currentConnection.getBoolean("comProxy")) {
                this.jtf_comInstanceNum.setText(currentConnection.getInteger("comInstanceNum").toString());
            }
        }
        if(type.equals(this.type_com_jrb.getActionCommand())) {
            this.jcb_serial.getModel().setSelectedItem(new ComboBoxItem(currentConnection.getString("url")));
            this.jcb_isManage.setSelected(currentConnection.containsKey("manage")?currentConnection.getBoolean("manage"):false);
            this.jtf_btl.setText(currentConnection.getString("deviceId"));
            this.jtf_sjw.setText(currentConnection.getString("username"));
            this.jtf_tzw.setText(currentConnection.getString("password"));
            this.jtf_xyw.setText(currentConnection.containsKey("xyw")?currentConnection.getString("xyw"):"0");
        }
    }//GEN-LAST:event_jb_getActionPerformed

    private void jtf_comInstanceNumKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_comInstanceNumKeyTyped
        JTextField jtf = (JTextField)evt.getSource();
        if (!TerminalApplication.numbers.contains(evt.getKeyChar())) {
            evt.consume();
        }
        try{
            Integer.parseInt(jtf.getText());
            if(jtf.getText().startsWith("0")){
                jtf.setText(jtf.getText().replaceAll("^0+", ""));
            }
        }catch (Exception e){
            jtf.setText("0");
        }
    }//GEN-LAST:event_jtf_comInstanceNumKeyTyped

    private void jtf_btlKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_btlKeyTyped
        jtf_comInstanceNumKeyTyped(evt);
    }//GEN-LAST:event_jtf_btlKeyTyped

    private void jtf_sjwKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_sjwKeyTyped
        jtf_comInstanceNumKeyTyped(evt);
    }//GEN-LAST:event_jtf_sjwKeyTyped

    private void jtf_tzwKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_tzwKeyTyped
        jtf_comInstanceNumKeyTyped(evt);
    }//GEN-LAST:event_jtf_tzwKeyTyped

    private void jtf_xywKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtf_xywKeyTyped
        jtf_comInstanceNumKeyTyped(evt);
    }//GEN-LAST:event_jtf_xywKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton jb_get;
    private javax.swing.JButton jb_save;
    private javax.swing.JCheckBox jcb_comProxy;
    private javax.swing.JCheckBox jcb_isManage;
    private javax.swing.JComboBox<ComboBoxItem> jcb_serial;
    private javax.swing.JLabel jl_device;
    private javax.swing.JLabel jl_name;
    private javax.swing.JLabel jl_password;
    private javax.swing.JLabel jl_url;
    private javax.swing.JLabel jl_username;
    private javax.swing.JRadioButton jrb_websocket;
    private javax.swing.JTextField jtf_btl;
    private javax.swing.JTextField jtf_comInstanceNum;
    private javax.swing.JTextField jtf_device;
    private javax.swing.JTextField jtf_name;
    private javax.swing.JTextField jtf_password;
    private javax.swing.JTextField jtf_sjw;
    private javax.swing.JTextField jtf_tzw;
    private javax.swing.JTextField jtf_url;
    private javax.swing.JTextField jtf_username;
    private javax.swing.JTextField jtf_xyw;
    private javax.swing.ButtonGroup type_bg;
    private javax.swing.JRadioButton type_com_jrb;
    private javax.swing.JRadioButton type_http_jrb;
    // End of variables declaration//GEN-END:variables
}
